package com.com.corejavalearnings.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.logging.Logger;

/**
 * Created by pankaj on 7/1/14.
 */
public class MethodArgumentAndThreading {

    static Logger logger = Logger.getLogger(MethodArgumentAndThreading.class.getName());

    static {
        logger.setParent(Logger.getLogger(MethodArgumentAndThreading.class.getPackage().getName()));
    }

    static Runnable task1 = new Runnable() {
        @Override
        public void run() {
            Employee e = new Employee(1);
            logger.info(Thread.currentThread().getName() + "---" + calculateTotal(e));

        }
    };
    static Thread t1 = new Thread(task1);

    static Runnable task2 = new Runnable() {
        @Override
        public void run() {
            Employee e = new Employee(2);
            logger.info(Thread.currentThread().getName() + "---" + calculateTotal(e));
        }
    };
    static Thread t2 = new Thread(task2);

    static Runnable task3 = new Runnable() {
        @Override
        public void run() {
            Employee e = new Employee(3);
            logger.info(Thread.currentThread().getName() + "---" + calculateTotal(e));
        }
    };
    static Thread t3 = new Thread(task3);

    static int calculateTotal(Employee e) {
        int sum = 0;
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
            try {
                Thread.sleep(random.nextInt(200));
            }catch (Exception ex){

            }
            sum = sum + e.itemBought();
        }
        return sum;
    }

    public static void main(String... a) throws Exception{
        t1.start();
        t2.start();
        t3.start();
    }
}

class Employee {
    static Map<String, Integer> rateList = new HashMap<String, Integer>() {{
        put("0", 2);
        put("1", 3);
        put("2", 4);
        put("3", 5);
        put("4", 6);
        put("6", 7);
    }};
    private String name;
    private String age;
    private int index;

    public Employee(int index) {
        this.index = index;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int itemBought() {
        return rateList.get(index + "");
    }
}